using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

using Acms.SqlDal;

namespace Acms.Bll
{
    public class UserPostVote : BaseItem
    {
        protected UserPostVote() : base() { }
        protected UserPostVote(Hashtable table) : base(table) { }
        protected UserPostVote(Hashtable table, List<UserPostVote> collection) : base(table, collection) { }
        
        public static UserPostVote CreateInstance()
        {
            return new UserPostVote();
        }

        public static UserPostVote CreateInstance(int userId, Guid postGuid)
        {
            UserPostVote tmp = new UserPostVote();
            tmp.userId = userId;
            tmp.postGuid = postGuid;
            return tmp;
        }

        private static UserPostVote Get(Hashtable table)
        {
            if (table != null)
            {
                return new UserPostVote(table);
            }
            else
            {
                return null;
            }
        }

        private static List<UserPostVote> Get(Hashtable[] tables)
        {
            if (tables != null)
            {
                List<UserPostVote> tmp = new List<UserPostVote>();
                foreach (Hashtable table in tables)
                {
                    tmp.Add(new UserPostVote(table, tmp));
                }
                return tmp;
            }
            else
            {
                return null;
            }
        }

        public static UserPostVote GetByUserIdAndPostGuid(int userId, Guid postGuid)
        {
            UserPostVoteDal dal = new UserPostVoteDal();
            return Get(dal.GetByUserIdAndPostGuid(userId, postGuid));
        }

        public static List<UserPostVote> GetByAllByPostGuid(Guid postGuid)
        {
            UserPostVoteDal dal = new UserPostVoteDal();
            return Get(dal.GetByAllByPostGuid(postGuid));
        }

        public new void Update()
        {
            base.Update();
        }

        protected override object SaveChanges(ref object concurencyId, Base dal)
        {
            if(IsNewRecord)
            {
                created = DateTime.Now;
            }
            ((UserPostVoteDal)dal).Update(ref id, userId, ref username, ref firstName, ref lastName, postGuid, score, created, ref concurencyId);
            return Id;
        }

        protected override void Load(Hashtable table)
        {
            id = (int)table[UserPostVoteDal.Fields.Id];
            userId = (int)table[UserPostVoteDal.Fields.UserId];
            username = (string)table[UserPostVoteDal.Fields.Username];
            firstName = (string)table[UserPostVoteDal.Fields.FirstName];
            lastName = (string)table[UserPostVoteDal.Fields.LastName];
            postGuid = (Guid)table[UserPostVoteDal.Fields.PostGuid];
            score = (decimal)table[UserPostVoteDal.Fields.Score];
            created = (DateTime)table[UserPostVoteDal.Fields.Created];
        }

        private int id;
        public int Id
        {
            get { return id; }
        }
	

        private int userId;
        public int UserId
        {
            get { return userId; }
        }

        private string username;
        public string Username
        {
            get { return username; }
        }

        private string firstName;
        public string FirstName
        {
            get { return firstName; }
        }

        private string lastName;
        public string LastName
        {
            get { return lastName; }
        }

        public string DisplayName
        {
            get
            {
                string tmp = string.Format("{0} {1}", firstName, lastName);
                tmp = tmp.Trim();
                while (tmp.Contains("  "))
                {
                    tmp = tmp.Replace("  ", " ");
                }
                return tmp;
            }
        }

        private Guid postGuid;
        public Guid PostGuid
	    {
            get { return postGuid; }
	    }

        private decimal score;
        public decimal Score
        {
            get { return score;}
            set { score = value; }
        }

        private DateTime created;
        public DateTime Created
        {
            get { return created; }
        }
	
        protected override Acms.SqlDal.Base CreateDal()
        {
            return new UserPostVoteDal();
        }

        public static void Sort(List<UserPostVote> list, string expression)
        {
            BaseItem.SortList(list, expression, new UserPostVoteComparison());
        }

    }

    internal class UserPostVoteComparison : BaseComparison<UserPostVote>
    {
        public override int Compare(UserPostVote x, UserPostVote y)
        {
            switch(Field.ToLower())
            {
                case "id":
                    return GetReturnValue(x.Id, y.Id);
                case "userid":
                    return GetReturnValue(x.UserId, y.UserId);
                case "username":
                    return GetReturnValue(x.Username, y.Username);
                case "firstname":
                    return GetReturnValue(x.FirstName, y.FirstName);
                case "lastname":
                    return GetReturnValue(x.LastName, y.LastName);
                case "score":
                    return GetReturnValue(x.Score, y.Score);
                case "created":
                    return GetReturnValue(x.Created, y.Created);
            }
            return 0;
        }
    }
}
